/*
https://leetcode-cn.com/problems/heaters/solution/gong-nuan-qi-by-leetcode-solution-rwui/
 */
import java.util.Arrays;

public class Solution475 {
    public int findRadius(int[] houses, int[] heaters) {
        Arrays.sort(houses);
        Arrays.sort(heaters);
        int ans=0;
        int j=0;
        for (int i=0;i<houses.length;i++){
            while (j<heaters.length && houses[i]>heaters[j]){
                j++;
            }
            if (j==0){
                ans=Math.max(ans,heaters[j]-houses[i]);
            }else if (j==heaters.length){
                ans=Math.max(ans,houses[i]-heaters[j-1]);
            }else{
                ans=Math.max(ans,Math.min(houses[i]-heaters[j-1],heaters[j]-houses[i]));
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution475().findRadius(new int[]{1,5},new int[]{2}));
    }
}
